package com.tucy.logistics.modules.assist.tool.controller;

import com.tucy.logistics.common.annotation.EnumAnnotation;
import com.tucy.logistics.common.base.EnumBean;
import com.tucy.logistics.common.base.EnumInterface;
import com.tucy.logistics.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.reflections.Reflections;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.*;

/**
 * author: tcy
 * createDate: 2023/1/31
 * description:公用工具方法接口
 */
@Api(tags = "公共方法")
@RestController
@RequestMapping("/tool")
public class ToolController {

    @ApiOperation(value = "获取枚举", notes = "获取枚举")
    @GetMapping("/getEnums")
    public Result<Map<String, List<EnumBean>>> getEnums() {
        Map<String, List<EnumBean>> enums = new HashMap<>();
        Reflections reflections = new Reflections("com.tucy.logistics.common");
        Set<Class<? extends EnumInterface>> monitorClasses = reflections.getSubTypesOf(EnumInterface.class);

        //System.out.println("size:"+monitorClasses.size());
        monitorClasses.forEach(m -> {
            try {
                //System.out.println("name:"+m.getSimpleName());

                List<EnumBean> enumBeans = new LinkedList<>();
                EnumInterface[] enumConstants = m.getEnumConstants();

                for (EnumInterface anEnum : enumConstants) {
                    //System.out.println("code:"+anEnum.getCode()+"  value:"+anEnum.getValue()+"  key:"+anEnum.getKey());
                    EnumBean enumBean =new EnumBean();
                    enumBean.setCode(anEnum.getCode());
                    enumBean.setKey(anEnum.getKey());
                    enumBean.setValue(anEnum.getValue());
                    enumBeans.add(enumBean);
                }
                enums.put(m.getSimpleName(),enumBeans);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return Result.success(enums);
    }

    @ApiOperation(value = "获取枚举", notes = "获取枚举")
    @GetMapping("/listEnumsByNote")
    public Result<Map<String, List<EnumBean>>> listEnumsByNote() throws Exception{
        /**
         * 这个方式将注解和接口全部用上，不如单用接口方便
         */
        Map<String, List<EnumBean>> enums = new HashMap<>();

        Reflections reflections = new Reflections("com.tucy.logistics.common");
        Set<Class<?>> monitorClasses = reflections.getTypesAnnotatedWith(EnumAnnotation.class);

        for (Class<?> classes : monitorClasses) {
            String clazzName = classes.getSimpleName();
            List<EnumBean> enumBeans = new LinkedList<>(); // 创建新的列表

            //下面两种方式获取EnumInterface[]，随意使用
            EnumInterface[] enumConstants = (EnumInterface[]) classes.getEnumConstants();
            EnumInterface[] enumInterfaces = (EnumInterface[]) classes.getMethod("values").invoke(null);

            if (enumInterfaces != null && enumInterfaces.length > 0) {
                for (EnumInterface anEnum : enumInterfaces) {
                    EnumBean enumBean = new EnumBean();
                    enumBean.setCode(anEnum.getCode());
                    enumBean.setKey(anEnum.getKey());
                    enumBean.setValue(anEnum.getValue());
                    enumBeans.add(enumBean);
                }
                enums.put(clazzName, enumBeans);
            }
        }
        return Result.success(enums);
    }
}
